﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using log4net;
using System.Reflection;

namespace Cn.Edwon.Web.WebService {
    public enum SoapResponseResult{
        Fault = 0,
        Ok = 1,
        Unknown = 9
    }
    public class SoapDataItem {

        public SoapDataItem() {
            _ResponseResult = SoapResponseResult.Fault;
            _Content = "";
        }

        private SoapResponseResult _ResponseResult;
        public SoapResponseResult ResponseResult {
            set { _ResponseResult = value; }
            get { return _ResponseResult; }
        }
        private String _Content;
        public String Content {
            set { _Content = value; }
            get { return _Content; }
        }
    }
    public class SoapHelper {
        public static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public static String BuildSoap(String bodyXmlContent) { 
            String str = "<?xml version=\"1.0\" ?>";
            str += "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">";
            str += bodyXmlContent;
            str += "</soap:Envelope>";
            return str;
        }
        /// <summary>
        /// 发送一个SOAP请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="bodyXmlContent"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static String Send(String url,String bodyXmlContent,Encoding encode) {
            String xml = BuildSoap(bodyXmlContent);
            XmlDocument doc = new XmlDocument();
            try {
                doc.LoadXml(xml);
                return HttpUtil.ParseSoapPostAction(url, xml, encode);
            } catch (XmlException xex) {
                log.Error("内容数据不是合法的XML结构数据:" + xex.Message);
                return ReturnFault("内容数据不是合法的XML结构数据");
            } catch (Exception ex) {
                log.Error("发送请求时出现异常:" + ex.Message);
                return ReturnFault("发送请求时出现异常");

            }
        }
        /// <summary>
        /// 返回一个失败的请求结果
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static String ReturnFault(String msg) {
            String str = "<?xml version=\"1.0\" ?>";
            str += "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">";
            str += "<soap:Fault>";
            str += "<soap:faultcode></soap:faultcode>";
            str += "<soap:faultstring></soap:faultstring>";
            str += "<soap:faultactor></soap:faultactor>";
            str += "<soap:detail><![CDATA[" + msg + "]]></soap:detail>";
            str += "</soap:Fault>";
            str += "</soap:Envelope>";
            return str;
        }
        /// <summary>
        /// 解析SOAP请求结果
        /// </summary>
        /// <param name="soap"></param>
        /// <returns></returns>
        public static SoapDataItem ParseSoapResponse(String soap) {
            SoapDataItem eo = new SoapDataItem();
            try {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(soap);
                XmlNamespaceManager xnm = new XmlNamespaceManager(new NameTable());
                xnm.AddNamespace("soap", "http://www.w3.org/2001/12/soap-envelope");
                XmlNode node = doc.SelectSingleNode("Fault", xnm);
                if (node != null) { //is fault
                    eo.ResponseResult = SoapResponseResult.Fault;
                    eo.ResponseResult = SoapResponseResult.Ok;
                    XmlNode fault = doc.SelectSingleNode("Fault/detail", xnm);
                    if (fault != null) {
                        eo.Content = fault.InnerXml;
                    } else {
                        eo.Content = "";
                    }
                } else {
                    eo.ResponseResult = SoapResponseResult.Ok;
                    XmlNode body = doc.SelectSingleNode("Body", xnm);
                    if (body != null) {
                        eo.Content = body.InnerXml;
                    } else {
                        eo.Content = "";
                    }
                }
            } catch (Exception ex) {
                log.Error("ParseSoapResponse exception:" + ex.Message);
                eo.ResponseResult = SoapResponseResult.Unknown;
            }
            return eo;
        }
    }
}
